import Header from "../../components/Header.js";
import Footer from "../../components/Footer.js";
import ArticleComponent from "../../components/Article";
import { findArticle } from "../api/articles";
import { useState } from "react";
export default function CueeentArticle({ article }) {
  const [articleData, setArticle] = useState(article);
  const setLike = (type) => {
    if (type === "add") {
      setArticle({ ...articleData, likeCount: articleData.likeCount + 1 });
      // console.log(article.likeCount, 'likeCount')
    }
  };

  return (
    <>
      <Header title={articleData.title}></Header>
      <ArticleComponent data={articleData} setLike={setLike}></ArticleComponent>
      <Footer></Footer>
    </>
  );
}

export const getStaticProps = async (context) => {
  const id = context.params.id; //拿到路由传递的参数，去获得对应的参数
  let result = await findArticle(id);
  const { data, message, code } = result;
  // console.log(data, message, code, 'result')
  return {
    props: {
      article: data,
    },
  };
};

export const getStaticPaths = async () => {
  const posts = [{ id: "1" }];
  return {
    paths: posts.map((post) => {
      return {
        params: {
          id: post.id,
        },
      };
    }),
    fallback: false,
  };
};
